package com.itic.mall.system.users.usercoupon.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.CouponState;
import com.itic.appbase.framework.type.CouponType;
import com.itic.mall.system.inter.project.coupon.persistence.model.ProjectCoupon;
import com.itic.mall.system.inter.users.usercoupon.persistence.dao.UsersCouponDao;
import com.itic.mall.system.inter.users.usercoupon.persistence.model.UsersCoupon;
import com.itic.mall.system.inter.users.usercoupon.persistence.model.UsersCouponExample;
import com.itic.mall.system.inter.users.usercoupon.persistence.model.UsersCouponExample.Criteria;
import com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMall;
import com.itic.mall.system.orders.item.service.OrdersItemService;
import com.itic.mall.system.project.coupon.service.ProjectCouponService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional
public class UsersCouponService
     extends CrudService<UsersCouponDao,UsersCoupon,UsersCouponExample> {
    @Autowired
    private UsersCouponDao usersCouponDao;
    @Autowired
    private OrdersItemService ordersItemService;
    @Autowired
    private ProjectCouponService projectCouponService;

    public  String subUsersCouponByCouponId(String couponId) {
    	UsersCoupon usersCoupon = this.get(couponId);
    	String result = SUCCESS;
    	if(usersCoupon!=null) {
    		usersCoupon.setIsUsed(DBConst.TRUE);
    		result = this.edit(usersCoupon);
    	}
    	return result;
    }
    
    public UsersCoupon getUsersCouponByCouponId(String couponId) {
    	UsersCouponExample example = new UsersCouponExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo("1");
    	criteria.andStartTimeLessThan(new Date());
    	criteria.andEndTimeGreaterThan(new Date());
    	criteria.andIdEqualTo(couponId);
    	List<UsersCoupon> usersCoupons = this.list(example);
    	if(usersCoupons.isEmpty())
    		return null;
    	else 
    		return usersCoupons.get(0);
    }
    
    
    public String addUsersCoupon(Date startDate,Date endDate,String couponId,String userId,String couponName,String type,String spuId,double amount,Double cond,CouponState couponState) {
    	UsersCoupon usersCoupon = new UsersCoupon();
    	usersCoupon.setCouponId(couponId);
    	usersCoupon.setUserId(userId);
    	usersCoupon.setCouponName(couponName);
    	usersCoupon.setStartTime(startDate);
    	usersCoupon.setEndTime(endDate);
    	usersCoupon.setType(type);
    	usersCoupon.setAmount(amount);
    	usersCoupon.setUseCond(cond);
    	usersCoupon.setSpuId(spuId);
    	usersCoupon.setSourceType(couponState.getType());
    	String result = this.add(usersCoupon);
    	return result;
    }
    
    public List<UsersCoupon> getUsersCouponByUserId(String userId,CouponState couponState,String isUse){
    	UsersCouponExample example = new UsersCouponExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andStartTimeLessThan(new Date());
    	criteria.andEndTimeGreaterThan(new Date());
    	if(StringUtils.isNotEmpty(isUse))
    		criteria.andIsUsedEqualTo(isUse);
    	criteria.andUserIdEqualTo(userId);
    	criteria.andSourceTypeEqualTo(couponState.getType());
    	List<UsersCoupon> usersCoupons = this.list(example);
    	return usersCoupons;
    }
    
    public double calCouponPrice(String couponId,double actualPay) {
		UsersCoupon usersCoupon = this.getUsersCouponByCouponId(couponId);
		if(usersCoupon !=null) {
			CouponType couponType = CouponType.getCouponType(usersCoupon.getType());
			actualPay = this.ordersItemService.calCouponMoney(couponType, actualPay, usersCoupon);
		}
    	return actualPay;
    }
    
    public List<UsersCoupon> getUserCouponByUserIdAndCouponId(String userId,String couponId) {
    	UsersCouponExample example = new UsersCouponExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	criteria.andCouponIdEqualTo(couponId);
    	List<UsersCoupon> usersCoupons = this.list(example);
    	return usersCoupons;
    }
    
    public void editUseUserCouponById(String couponId) {
    	UsersCouponExample example = new UsersCouponExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andCouponIdEqualTo(couponId);
    	List<UsersCoupon> usersCoupons = this.list(example);
    	for(UsersCoupon coupon : usersCoupons) {
    		coupon.setIsUsed("1");
    		this.edit(coupon);
    	}
    }
    
    public List<UsersCoupon> findCouponListByUserIdAndSpuId(String userId,String spuId){
    	UsersCouponExample example = new UsersCouponExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andSpuIdEqualTo(spuId);
    	criteria.andUserIdEqualTo(userId);
    	List<UsersCoupon> usersCoupons = this.list(example);
    	return usersCoupons;
    }
    /**
     * 新注册用户添加红包
     * @param usersMall
     * @return
     */
    public String addUserRegistCoupon(UsersMall usersMall) {
    	String result =SUCCESS;
    	List<ProjectCoupon> projectCoupons = this.projectCouponService.getRegistCoupon();
    	for(ProjectCoupon projectCoupon : projectCoupons) {
    		result = this.addUsersCoupon(projectCoupon.getStartTime(),projectCoupon.getEndTime(), projectCoupon.getId(), usersMall.getId(), projectCoupon.getName(), projectCoupon.getType(),null,projectCoupon.getAmount(), projectCoupon.getUseCond(), CouponState.sys);
    	}
    	return result;
    }
  
}